﻿using Microsoft.Extensions.DependencyInjection.Extensions;
using Sgr.EntityFrameworkCore;
using Sgr.OmniMetrics.Infrastructure;
using Sgr.OmniMetrics.Services;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddOmniMetricsDatabaseStore(this IServiceCollection services)
        {
            services.TryAddEnumerable(ServiceDescriptor.Singleton<IEntityFrameworkTypeProvider, OmniMetricsEntityFrameworkTypeProvider>());

            if (services.Any(f => f.ServiceType == typeof(IOmniMetricService)))
                services.Replace(ServiceDescriptor.Scoped<IOmniMetricService, OmniMetricUseDatabaseService>());
            else
                services.AddScoped<IOmniMetricService, OmniMetricUseDatabaseService>();

            return services;
        }
    }
}